﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using BeautyTech.Core.Domain.Catalogs;
using BeautyTech.Services.Catalogs;
using BeautyTech.WebSite.Models.Catalog;
using AutoMapper;


namespace BeautyTech.WebSite.Controllers
{
    public class CatalogController : Controller
    {
        #region Fields

        private readonly ICategoryService _categoryService;
        private readonly IProductService _productService;

        #endregion

        #region Constructors

        public CatalogController(ICategoryService categoryService, IProductService productService)
        {
            this._categoryService = categoryService;
            this._productService = productService;
        }

        #endregion

        public ViewResult Index(int id)
        {
            var categorie = _categoryService.GetCategoryById(id);

            // Mapping de "Category" vers le ViewModel "CategoryModel"
            Mapper.CreateMap<Category, CategoryModel>();
            CategoryModel categ = Mapper.Map<Category, CategoryModel>(categorie);

            return View(categ);
        }

        #region Categories

        [ChildActionOnly]
        public PartialViewResult HomepageCategories()
        {
            var mainCateg = _categoryService.GetAllCategoriesDisplayedOnHomePage();

            // Mapping de "IList<Category>" vers le ViewModel "IList<CategoryModel>"
            Mapper.CreateMap<Category, CategoryModel>();
            IList<CategoryModel> ListModel = Mapper.Map<IList<Category>, IList<CategoryModel>>(mainCateg);

            return PartialView(ListModel);

        }

        [ChildActionOnly]
        public PartialViewResult ChildCategories(int id)
        {
            var mainCateg = _categoryService.GetAllCategoriesByParentCategoryId(id);

            // Mapping de "IList<CategoryModel>" vers le ViewModel "IList<CategoryModel>"
            Mapper.CreateMap<Category, CategoryModel>();
            IList<CategoryModel> ListModel = Mapper.Map<IList<Category>, IList<CategoryModel>>(mainCateg);

            return PartialView(ListModel);
        }

        
        #endregion

        #region Products

        public ActionResult Product(string id)
        {
            var product = _productService.GetProductByReference(id);
            if (product == null)
                return null;

            // Mapping
            Mapper.CreateMap<Product, ProductModel>();
            ProductModel model = Mapper.Map<Product, ProductModel>(product);

            //save as recently viewed

            return View(model);
        }

        [ChildActionOnly]
        public PartialViewResult ProductBreadcrumb()
        {
            ViewBag.ArticleName = "test";
            return PartialView();
        }
        
        [ChildActionOnly]
        public ActionResult RelatedProducts(int productId, int? productThumbPictureSize)
        {
            var products = new List<Product>();
            var relatedProducts = _productService.GetRelatedProductsByProductId1(productId);
            foreach (var product in _productService.GetProductsByIds(relatedProducts.Select(x => x.ProductId2).ToArray()))
            {
                products.Add(product);
            }
            //var model = PrepareProductOverviewModels(products, true, true, productThumbPictureSize).ToList();
            // Mapping
            Mapper.CreateMap<Product, ProductOverviewModel>();
            IList<ProductOverviewModel> ListModel = Mapper.Map <IList<Product>, IList<ProductOverviewModel>>(products);

            return PartialView(ListModel);
        }

        #endregion

    }
}
